package com.boxin.ims.modules.bi.statics.web;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.bi.dao.BIDao;
import com.boxin.ims.modules.bi.entity.Series;
import com.boxin.ims.modules.bi.entity.Tooltip;
import com.boxin.ims.modules.bi.recharge.dao.BIRechargeDao;
import com.boxin.ims.modules.bi.statics.dao.BIStaticsDao;
import com.boxin.ims.modules.bi.utils.DateProcessingUtils;
import com.boxin.ims.modules.sys.scheduler.BiSceneProcessScheduler;
import com.boxin.ims.modules.wechat.entity.WechatScene;
import com.boxin.ims.modules.wechat.service.WechatSceneService;
import com.boxin.ims.modules.wechat.utils.AssertUtils;
import com.boxin.ims.modules.wechat.utils.XMLUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

import freemarker.template.utility.DateUtil;

/**
 * 用户充值统计
 * @author xiaoqian
 *
*	2014上午9:02:59
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/bi/statics")
public class BIStaticsController extends BaseController {
	
	
	@Autowired
	private BIStaticsDao biStaticsDao;
	
	
	@Autowired
	BiSceneProcessScheduler biSceneProcessScheduler;
	
	
	
	@RequestMapping(value ="biStatUsedList")
	public String biStatUsedList( HttpServletRequest request, HttpServletResponse response, Model model) throws ParseException {
		String time =request.getParameter("time");
		String terminal_name=request.getParameter("terminal_name");
		
		Calendar cal = Calendar.getInstance();
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		String year="";
		String month="";
		String today = null;
		String day="";
		if(time==null||time==""){
			today=format.format(new Date());
			month=DateProcessingUtils.getmonth();
			day=cal.get(Calendar.DAY_OF_MONTH)+"";
			year=cal.get(Calendar.YEAR)+"";
			
		}else{
			today = time;
			year=time.substring(0,4);
			month=time.substring(5,7);
			if(Integer.parseInt(month)<10){
				month=0+month;
			}
			day=time.substring(8,10);
		}
	
		
		User user=UserUtils.getUser();
		
		
		String sellerIds=request.getParameter("sellerIds");
		if(AssertUtils.notEmpty(sellerIds)){
			String[] userIds= sellerIds.split(",");
			if (user.isAdmin()){
				if(userIds.length==1){
					user=new User(Long.valueOf(userIds[0]));
					model.addAttribute("user", user);
				}
			}
		}
		
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("user_id", user.getId());
		
		 
		  Date date = format.parse(today);
		  Calendar calendar = Calendar.getInstance();
		  calendar.setFirstDayOfWeek(Calendar.MONDAY);
		  calendar.setTime(date);
		  System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
		  
		params.put("time",today);
		
		if(StringUtils.isNotEmpty(terminal_name)){
			if(!terminal_name.equals("0")){
				params.put("terminal_name",terminal_name);
			}
			
		}else{
			terminal_name="0";
		}
		
		
		List<Map<String, Object>> TerminalList=biStaticsDao.queryTerminalByUserId(params);
		
		List<Map<String, Object>> AllUsedAwardsList=biStaticsDao.queryAllUsedAwards(params);
		
		
		model.addAttribute("title","核销统计" );
		
		Set<String> set = new HashSet<String>();
		for (int i = 0; i < AllUsedAwardsList.size(); i++) {
			if(AllUsedAwardsList.get(i).get("NAME")!=null&&AssertUtils.notNullOrEmpty(AllUsedAwardsList.get(i).get("NAME").toString())){
				set.add(AllUsedAwardsList.get(i).get("NAME").toString());
			}
		}
		
		StringBuffer stringBuffer = new StringBuffer();
		
		stringBuffer.append("[");
		
		if(set.size()!=0){
			
			for (String str : set) {
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				for (int j = 0; j < AllUsedAwardsList.size(); j++) {
					if(AllUsedAwardsList.get(j).get("NAME")!=null&&AllUsedAwardsList.get(j).get("update_time")!=null&&AssertUtils.notNullOrEmpty(AllUsedAwardsList.get(j).get("NAME").toString())){
						if(AllUsedAwardsList.get(j).get("NAME").toString().equals(str)){
							list.add(AllUsedAwardsList.get(j));
						}
					}
				}
				Integer[] data=DateProcessingUtils.getDataByWeek(list,format.parse(today));
				
				
				Series series= new Series();
				series.setName(str);
				series.setType("column");
				series.setData(data);
				series.setPointStartByDay(year,(Integer.parseInt(month)-1)+"",Integer.parseInt(day)-3+"" );
				stringBuffer.append(DateProcessingUtils.getSeriesJson(series)).append(",");
			}
		}else{
			model.addAttribute("title","数据有误" );
			model.addAttribute("user",user);
			model.addAttribute("data", 0);
			model.addAttribute("xAxis", 0);
		}
		
		
		
		stringBuffer.append("]");
		
		model.addAttribute("time",today);
		model.addAttribute("series", stringBuffer);
		
		model.addAttribute("TerminalList", TerminalList);
		model.addAttribute("terminal_name", terminal_name);
		
		
		return "modules/bi/biStatUsedList";
	}
	
}

